package com.ruoyi.business.juheController;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DO.RetrievalSearchResult;
import com.ruoyi.system.domain.vo.R;
import com.ruoyi.system.protocol.IDynamicDataService;
import com.ruoyi.system.protocol.IDynamicSearchService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("ownJuheApi/statistics")
public class JuheStatisticsController {

    @Autowired
    @Qualifier("dynamicSearchJuheServiceImpl")
    private IDynamicSearchService dynamicSearchJuheServiceImpl;
    @Autowired
    private IDynamicDataService dynamicDataService;
    @Autowired
    private RedisCache redisCache;

    private final static String INDEX = "juhe";
    private final static String DYNAMIC_INDEX = "juhe_statistics";

    private final static String CLASSESEN = "weipinhui";

    @Autowired
    @Qualifier("juheJdbcTemplate39")
    private JdbcTemplate juheJdbcTemplate39;

    @Autowired
    @Qualifier("juheJdbcTemplate182")
    private JdbcTemplate juheJdbcTemplate182;


    @RequestMapping("/list")
    public R list() {
        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        int pageNum = ServletUtils.getParameterToInt("pageNum");
        int pageSize = ServletUtils.getParameterToInt("pageSize");
        String channelId = ServletUtils.getParameter("channelId");
        String mki = ServletUtils.getParameter("mki");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(businessTypeStr)){
            boolQueryBuilder.must(QueryBuilders.termQuery("type.keyword", businessTypeStr));
        }
        if(StringUtils.isNotEmpty(mki)){
            boolQueryBuilder.must(QueryBuilders.termQuery("id.keyword", mki));
        }
        if(StringUtils.isNotEmpty(channelId)){
            boolQueryBuilder.must(QueryBuilders.termQuery("channelId.keyword", channelId));
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            boolQueryBuilder.must(new RangeQueryBuilder("createTime.keyword").gte(beginTime).lte(endTime).format("yyyy-MM-dd HH:mm:ss"));
        }
        Map<String, String> sortField = new HashMap<>();
        sortField.put("createTime.keyword", "desc");
        HashMap<String, Object> search = dynamicSearchJuheServiceImpl.search(INDEX, boolQueryBuilder, null, null, sortField, null, pageNum, pageSize);
        return R.success().dataAll(search);
    }

    @RequestMapping("/channelList")
    public R channelList() {
        List<Map<String, Object>> channelAll = new ArrayList<>();
        List<Map<String, Object>> mapList39 = juheJdbcTemplate39.queryForList("select * from channel");
        List<Map<String, Object>> mapList182 = juheJdbcTemplate182.queryForList("select * from channel");
        channelAll.addAll(mapList39);
        channelAll.addAll(mapList182);
        //根据id去重
        Map<String, Map<String, Object>> map = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelAll) {
            stringObjectMap.put("title", stringObjectMap.get("channel_name"));
            map.put(stringObjectMap.get("id").toString(), stringObjectMap);
        }
        List<Map<String, Object>> list = new ArrayList<>(map.values());
        return R.success().data("list", list);
    }


    @RequestMapping("/statisticsTime")
    public R statisticsTime() {
        String channelId = ServletUtils.getParameter("channelId");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");
        String dimension = ServletUtils.getParameter("dimension");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(channelId)){
            boolQueryBuilder.must(QueryBuilders.termQuery("channelId.keyword", channelId));
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            boolQueryBuilder.must(new RangeQueryBuilder("createTime.keyword").gte(beginTime).lte(endTime).format("yyyy-MM-dd HH:mm:ss"));
        }


        //2024-02-03 19:17:48
        String idOrCode = "doc['createTime.keyword'].value";
        if("createTimeMap.y".equals(dimension)){
            idOrCode = "doc['createTime.keyword'].value.substring(0,4)";
        }else if("createTimeMap.ym".equals(dimension)){
            idOrCode = "doc['createTime.keyword'].value.substring(0,7)";
        }else if("createTimeMap.ymd".equals(dimension)){
            idOrCode = "doc['createTime.keyword'].value.substring(0,10)";
        }else if("createTimeMap.ymdh".equals(dimension)){
            idOrCode = "doc['createTime.keyword'].value.substring(0,13)";
        }else if("createTimeMap.ymdhm".equals(dimension)){
            idOrCode = "doc['createTime.keyword'].value.substring(0,16)";
        }

        Script script = new Script(Script.DEFAULT_SCRIPT_TYPE, Script.DEFAULT_SCRIPT_LANG, idOrCode, Collections.emptyMap());

        // Build the aggregation
        TermsAggregationBuilder dimensionAgg = AggregationBuilders.terms(dimension)
                .script(script).order(BucketOrder.key(false))
                .size(10);

        Map<String, Object> map = dynamicSearchJuheServiceImpl.analysisQuery(INDEX, boolQueryBuilder, dimensionAgg);
        return R.success().data("facet", map);
    }


    @RequestMapping("/statisticsChannel")
    public R statisticsChannel() {
        R r = channelList();
        List<Map<String, Object>> channelData = (List<Map<String, Object>>) r.getData().get("list");
        //id为key, map为value
        Map<String, Map<String, Object>> channelCacheMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelData) {
            channelCacheMap.put(stringObjectMap.get("id").toString(), stringObjectMap);
        }


        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        String channelId = ServletUtils.getParameter("channelId");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(businessTypeStr)){
            boolQueryBuilder.must(QueryBuilders.termQuery("type.keyword", businessTypeStr));
        }
        if(StringUtils.isNotEmpty(channelId)){
            boolQueryBuilder.must(QueryBuilders.termQuery("channelId.keyword", channelId));
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            boolQueryBuilder.must(new RangeQueryBuilder("createTime.keyword").gte(beginTime).lte(endTime).format("yyyy-MM-dd HH:mm:ss"));
        }
        LinkedHashMap<String, LinkedHashMap<String, Object>> facet = dynamicSearchJuheServiceImpl.facet(INDEX, null, boolQueryBuilder, new String[]{"channelId"}, 100);
        if(facet != null && facet.containsKey("channelId")){
            LinkedHashMap<String, Object> channelFacetNew = new LinkedHashMap<>();
            LinkedHashMap<String, Object> channelFacet = facet.get("channelId");

            Map<String, Object> channelDetail = null;
            for(String key: channelFacet.keySet()){
                channelDetail = (Map<String, Object>)channelCacheMap.get(key);
                if(channelDetail != null){
                    channelFacetNew.put("渠道："+channelDetail.get("title")+"", channelFacet.get(key));
                }else{
                    //没有查询到对应的渠道就不显示了
//                    channelFacetNew.put(key, channelFacet.get(key));
                }
            }
            facet.put("channelId", channelFacetNew);
        }
        return R.success().data("facet", facet);
    }


    @RequestMapping("/statisticsChannelHistory")
    public R statisticsChannelHistory() {
        R r = channelList();
        List<Map<String, Object>> channelData = (List<Map<String, Object>>) r.getData().get("list");
        //id为key, map为value
        Map<String, Map<String, Object>> channelCacheMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelData) {
            channelCacheMap.put(stringObjectMap.get("id").toString(), stringObjectMap);
        }


        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        String channelId = ServletUtils.getParameter("channelId");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");


        Map<String, Object> conditon = new HashMap<>();
        if(StringUtils.isNotBlank(businessTypeStr)){
            conditon.put("statistics_type", businessTypeStr);
        }
        if(StringUtils.isNotBlank(channelId)){
            conditon.put("channel_id", channelId);
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            conditon.put(">=,statistics_time", beginTime);
            conditon.put("<=,statistics_time", endTime);
        }

        Map<String, Object> sortCondition = new HashMap<>();
        sortCondition.put("statistics_time", "desc");

        Long dynamicDataCount = dynamicDataService.getDynamicDataCount(DYNAMIC_INDEX, conditon);
        List<Map<String, Object>> dynamicData = dynamicDataService.getDynamicData(DYNAMIC_INDEX, null, conditon, sortCondition, 1, dynamicDataCount.intValue());

        List<Map<String, String>> result = new ArrayList<>();

        //根据channelId分组
        Map<String, List<Map<String, Object>>> channelGroup = dynamicData.stream().collect(Collectors.groupingBy(e -> e.get("channel_id").toString()));
        //将分组内部的statistics_value相加
        for (String key : channelGroup.keySet()) {
            List<Map<String, Object>> maps = channelGroup.get(key);
            Long sum = maps.stream().mapToLong(e -> Long.parseLong(e.get("statistics_val").toString())).sum();

            Map<String, String> map = new HashMap<>();
            map.put("channelId", key);
            if(channelCacheMap.get(key) == null){
                map.put("channelName", key);
            }else{
                map.put("channelName", channelCacheMap.get(key).get("title").toString());
            }
            map.put("statisticsValue", sum.toString());
            result.add(map);
        }
        return R.success().data("list", result);
    }

    @RequestMapping("/statisticsBusinessTypeHistory")
    public R statisticsBusinessTypeHistory() {

        R r = channelList();
        List<Map<String, Object>> channelData = (List<Map<String, Object>>) r.getData().get("list");
        //id为key, map为value
        Map<String, Map<String, Object>> channelCacheMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelData) {
            channelCacheMap.put(stringObjectMap.get("id").toString(), stringObjectMap);
        }


        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        String channelId = ServletUtils.getParameter("channelId");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");


        Map<String, Object> conditon = new HashMap<>();
        if(StringUtils.isNotBlank(businessTypeStr)){
            conditon.put("statistics_type", businessTypeStr);
        }
        if(StringUtils.isNotBlank(channelId)){
            conditon.put("channel_id", channelId);
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            conditon.put(">=,statistics_time", beginTime);
            conditon.put("<=,statistics_time", endTime);
        }

        Map<String, Object> sortCondition = new HashMap<>();
        sortCondition.put("statistics_time", "desc");

        Long dynamicDataCount = dynamicDataService.getDynamicDataCount(DYNAMIC_INDEX, conditon);
        List<Map<String, Object>> dynamicData = dynamicDataService.getDynamicData(DYNAMIC_INDEX, null, conditon, sortCondition, 1, dynamicDataCount.intValue());
        //根据channelId和type分组
        Map<String, Map<String, List<Map<String, Object>>>> channelGroup = dynamicData.stream().collect(Collectors.groupingBy(e -> e.get("channel_id").toString(), Collectors.groupingBy(e -> e.get("statistics_type").toString())));
        List<Map<String, Object>> result = new ArrayList<>();
        for (String key : channelGroup.keySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("channelId", key);
            if(channelCacheMap.get(key) == null){
                map.put("channelName", key);
            }else{
                map.put("channelName", channelCacheMap.get(key).get("title").toString());
            }
            Map<String, List<Map<String, Object>>> stringListMap = channelGroup.get(key);
            for (String type : stringListMap.keySet()) {
                List<Map<String, Object>> maps = stringListMap.get(type);
                Long sum = maps.stream().mapToLong(e -> Long.parseLong(e.get("statistics_val").toString())).sum();
                map.put(type, sum);
            }
            result.add(map);
        }

        return R.success().data("list", result);
    }


    @RequestMapping("/statisticsBusinessType")
    public R statisticsBusinessType() {

        R r = channelList();
        List<Map<String, Object>> channelData = (List<Map<String, Object>>) r.getData().get("list");
        //id为key, map为value
        Map<String, Map<String, Object>> channelCacheMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelData) {
            channelCacheMap.put(stringObjectMap.get("id").toString(), stringObjectMap);
        }

        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        String channelId = ServletUtils.getParameter("channelId");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(businessTypeStr)){
            boolQueryBuilder.must(QueryBuilders.termQuery("type.keyword", businessTypeStr));
        }
        if(StringUtils.isNotEmpty(channelId)){
            boolQueryBuilder.must(QueryBuilders.termQuery("channelId.keyword", channelId));
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            boolQueryBuilder.must(new RangeQueryBuilder("createTime.keyword").gte(beginTime).lte(endTime).format("yyyy-MM-dd HH:mm:ss"));
        }

        TermsAggregationBuilder channelIdAgg = AggregationBuilders.terms("channelId").field("channelId.keyword").size(100);
        TermsAggregationBuilder businessTypeStrAgg = AggregationBuilders.terms("type").field("type.keyword");
        channelIdAgg.subAggregation(businessTypeStrAgg);
        Map<String, Object> map = dynamicSearchJuheServiceImpl.analysisQuery(INDEX, boolQueryBuilder, channelIdAgg);

        Map<String, Object> mapNew = new HashMap<>();
        if(map != null){
            for(String pkey: map.keySet()){
                mapNew = new HashMap<>();
                Map<String, Object> channelAggMap = (Map<String, Object>)map.get(pkey);
                Map<String, Object> channelDetail = null;
                for(String key: channelAggMap.keySet()){
                    channelDetail = (Map<String, Object>)channelCacheMap.get(key);
                    if(channelDetail != null){
                        mapNew.put(channelDetail.get("title")+"", channelAggMap.get(key));
                    }else{
                        //没有查询到对应的渠道就不显示了
//                        mapNew.put(key, channelAggMap.get(key));
                    }
                }
                map.put(pkey, mapNew);
            }

        }
        return R.success().data("agg", map);
    }


    @RequestMapping("/exportCsv")
    public void exportCsv(HttpServletResponse response) throws UnsupportedEncodingException {
        String businessTypeStr = ServletUtils.getParameter("businessTypeStr");
        int pageNum = ServletUtils.getParameterToInt("pageNum");
        int pageSize = ServletUtils.getParameterToInt("pageSize");
        String channelId = ServletUtils.getParameter("channelId");
        String mki = ServletUtils.getParameter("mki");
        String beginTime = ServletUtils.getParameter("beginTime");
        String endTime = ServletUtils.getParameter("endTime");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        if(StringUtils.isNotEmpty(businessTypeStr)){
            boolQueryBuilder.must(QueryBuilders.termQuery("type.keyword", businessTypeStr));
        }
        if(StringUtils.isNotEmpty(mki)){
            boolQueryBuilder.must(QueryBuilders.termQuery("id.keyword", mki));
        }
        if(StringUtils.isNotEmpty(channelId)){
            boolQueryBuilder.must(QueryBuilders.termQuery("channelId.keyword", channelId));
        }
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            boolQueryBuilder.must(new RangeQueryBuilder("createTime.keyword").gte(beginTime).lte(endTime).format("yyyy-MM-dd HH:mm:ss"));
        }
        Map<String, String> sortField = new HashMap<>();
        sortField.put("createTime.keyword", "desc");
        RetrievalSearchResult list = dynamicSearchJuheServiceImpl.list(INDEX, boolQueryBuilder, null, null, sortField, null, pageNum, pageSize);
        List<Map<String, Object>> records = list.getRecords();


        // 指定 CSV 文件名
        String csvFileName = "data_"+ DateUtils.dateTimeNow() +".csv";
        response.setContentType("text/csv;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(csvFileName, Constants.UTF8));
        Set<String> fields = ((Map<String, Object>) records.get(0).get("params")).keySet();

        // 创建 FileWriter 对象
        try (PrintWriter writer = response.getWriter()) {
            // 写入标题行
            writer.write(String.join(",", fields));
            writer.write("\n");

            // 写入数据行
            for (Map<String, Object> data: records) {
                Map<String, Object> paramsMap = (Map<String, Object>) data.get("params");
                for(String field: fields){
                    writer.write(new String((paramsMap.get(field)+"").getBytes("UTF-8")));
                    writer.write(",");
                }
                writer.write("\n");
            }

            writer.flush();
            System.out.println("CSV file created successfully.");
        } catch (IOException e) {
            System.out.println("Failed to create CSV file: " + e.getMessage());
        }
    }

    @RequestMapping("/importExcel")
    public void test() {
        R r = channelList();
        List<Map<String, Object>> channelData = (List<Map<String, Object>>) r.getData().get("list");
        //channel_name为key, map为value
        Map<String, Map<String, Object>> channelCacheMap = new HashMap<>();
        for (Map<String, Object> stringObjectMap : channelData) {
            channelCacheMap.put(stringObjectMap.get("channel_name").toString(), stringObjectMap);
        }

        List<Map<String, Object>> addList = new ArrayList<>();

        String filePath = "F:\\my\\11月开始点击";
        //遍历读取文件夹下的所有文件
        File file = new File(filePath);
        File[] files = file.listFiles();
        for (File fileIndex : files) {
            if (fileIndex.isDirectory()) {
                System.out.println("文件夹:" + fileIndex.getAbsolutePath());
            } else {
                System.out.println("文件:" + fileIndex.getAbsolutePath());
                String fileName = fileIndex.getName();
                String timeKey = fileName.substring(0, 10);
                String typeStr = fileName.substring(fileName.indexOf(".")-2, fileName.indexOf("."));
                String type = "click";
                if("激活".equals(typeStr)){
                    type = "callback";
                }
                //fileIndex 转为 fileInputStream
                try {
                    FileInputStream fileInputStream = new FileInputStream(fileIndex);
                    List<Map<String, Object>> mapList = readExcel(fileInputStream);
                    mapList = mapList.stream().filter(
                            x -> !"-".equals(x.get("产品") + "") && !"-".equals(x.get("渠道号") + ""))
                            .collect(Collectors.toList());
                    //mapList根据渠道号分组
                    Map<String, Integer> resMap = mapList.stream()
                            .collect(Collectors.groupingBy(x -> x.get("渠道号") + "", Collectors.summingInt(x -> {
                                String numStr = x.get("当天日期") + "";
                                return Integer.parseInt(numStr.substring(0, numStr.indexOf(".")));
                            })));
                    //遍历resMap,根据渠道号查询渠道名称
                    for(String key: resMap.keySet()){
                        Map<String, Object> channelMap = channelCacheMap.get(key);
                        if(channelMap != null){
                            Map<String, Object> addMap = new HashMap<>();
                            addMap.put("statistics_time", timeKey);
                            addMap.put("channel_id", channelMap.get("id").toString());
                            addMap.put("statistics_type", type);
                            addMap.put("statistics_val", resMap.get(key));
                            addList.add(addMap);
                        }
                    }
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        System.out.println(JSONObject.toJSONString(addList));
        //addList根据channel_id和statistics_time分组
        Map<String, List<Map<String, Object>>> resMap = addList.stream()
                .collect(Collectors.groupingBy(x -> x.get("channel_id") + "-" + x.get("statistics_time")));
        //判断resMap中value的size是否有不等于2的
        for(String key: resMap.keySet()){
            if(resMap.get(key).size() != 2){
                throw new RuntimeException("数据不完整");
            }
        }
        System.out.println("===================数据完整==================");

        List<Map<String, Object>> adddbList = new ArrayList<>();
        List<Map<String, Object>> removedbList = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : addList) {
            String channelId = stringObjectMap.get("channel_id") + "";
            String statisticsTime = stringObjectMap.get("statistics_time") + "";
            Map<String, Object> condition = new HashMap<>();
            condition.put("channel_id", channelId);
            condition.put("statistics_time", statisticsTime);
            Long dynamicDataCount = dynamicDataService.getDynamicDataCount(DYNAMIC_INDEX, condition);
            if(dynamicDataCount == 0){
                adddbList.add(stringObjectMap);
            }else{
                removedbList.add(stringObjectMap);
            }
        }
        System.out.println(adddbList.size());

        dynamicDataService.addBatchDynamicData(DYNAMIC_INDEX, adddbList);
    }


    public List<Map<String, Object>> readExcel(FileInputStream fileInputStream) {
        List<Map<String, Object>> data = new ArrayList<>();
        try {
            List<String> fields = new ArrayList<>();
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
            //获取最后一行的num，即总行数。此处从0开始
            int maxRow = sheet.getLastRowNum();
            for (int row = 0; row <= maxRow; row++) {
                //获取最后单元格num，即总单元格数 ***注意：此处从1开始计数***
                int maxRol = sheet.getRow(row).getLastCellNum();
                Map<String, Object> map = new HashMap<>();
                for (int rol = 0; rol < maxRol; rol++) {
                    if (row == 0) {
                        fields.add(sheet.getRow(row).getCell(rol).toString());
                    } else {
                        map.put(fields.get(rol), sheet.getRow(row).getCell(rol).toString());
                    }
                }
                if(row != 0){
                    data.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    public static void main(String[] args) {

        String a = "1.000000";
        //转为int
        System.out.println(Integer.parseInt(a.substring(0, a.indexOf("."))));
    }

}
